from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .ser import *
# Create your views here.

class Meman(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Me.objects.filter(username=username).first()
        if user.username==username:
            if user.password==password:
                return Response({'code':200,'userid':user.id,'username':user.username})
            else:
                return Response({'code':20010,'meg':'密码错误'})
        else:
            return Response({'code': 20011, 'meg': '用户不存在'})

class SchoolM(APIView):
    def post(self,request):
        ser = Schoolser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code':200})
        else:
            print(ser.errors)
            return Response({'code':20010})
    def get(self,request):
        se = School.objects.all()
        ser = Schoolser(se,many=True)
        return Response({'code':200,'list':ser.data})

class SclassM(APIView):
    def post(self,request):
        ser = Sclassser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code':200})
        else:
            print(ser.errors)
            return Response({'code':20010})
    def get(self,request):
        se = Sclass.objects.all()
        ser = Sclassser(se,many=True)
        return Response({'code':200,'list':ser.data})

class StudenM(APIView):
    def post(self,request):
        ser = Studentser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code':200})
        else:
            print(ser.errors)
            return Response({'code':20010})
    def get(self,request):
        se = Student.objects.all()
        ser = Studentser(se,many=True)
        return Response({'code':200,'list':ser.data})